#include "tann.h"
#include "./ui_tann.h"
#include<Qpainter>
#include<QTimer>
#include<QKeyEvent>
tann::tann(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::tann),blsRun(false),Speed(500)//速度初始化
{
    ui->setupUi(this);
    this->setGeometry(QRect(500,100,600,630));
}

tann::~tann()
{
    delete ui;
}

void tann::paintEvent(QPaintEvent*event){
    QPainter painter(this);
    if(!blsRun)
        InitSnake();
    //外墙
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(15,15,570,570);

    //内墙
    painter.setPen(Qt::black);
    painter.setBrush(Qt::white);
    painter.drawRect(20,20,560,560);

    //画格子
    painter.setPen(Qt::blue);
    for(int i=2;i<=58;i++){
        painter.drawLine(20,i*10,580,i*10);
        painter.drawLine(i*10,20,i*10,580);
    }
    //显示游戏开始和结束
    QFont font1("Courier",26);
    painter.setFont(font1);
    painter.setPen(Qt::red);
    painter.setBrush(Qt::red);
    painter.drawText(250,300,Display);
    //显示分数
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(300,610,"得分：");
    painter.drawText(400,610,QString::number(Score));
    //画蛇
    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    painter.drawPixmap(Food,QPixmap(":/img"));
    painter.drawRects(&vSnakeRect[0],vSnakeRect.size());

    if(blsOver)
        timer->stop();


}

void tann::InitSnake(){
    blsRun=true;
    blsOver=false;
    nDirection=2;
    Display ="游戏开始";
    Score=0;
    Food=CreateFood();
    vSnakeRect.resize(5);
    //初始化蛇的位置
    for(int i=0;i<vSnakeRect.size();i++){
        QRect rect(100,70+10*i,10,10);
        vSnakeRect[vSnakeRect.size()-i-1]=rect;
    }

    timer=new QTimer(this);
    timer->start(Speed);
    connect(timer,SIGNAL(timeout()),SLOT(Snake_update()));

}
// 每次更新函数
void tann::Snake_update(){
    Display="";
    SnakeHead=vSnakeRect.first();
    IsEat();
    IsHit();
    for(int i=0;i<vSnakeRect.size()-1;i++){
        vSnakeRect[vSnakeRect.size()-1-i]=vSnakeRect[vSnakeRect.size()-2-i];
    }
    switch(nDirection){
    case 1:
        SnakeHead.setTop(SnakeHead.top()-10);
        SnakeHead.setBottom(SnakeHead.bottom()-10);
        break;
    case 2:
        SnakeHead.setTop(SnakeHead.top()+10);
        SnakeHead.setBottom(SnakeHead.bottom()+10);
        break;
    case 3:
        SnakeHead.setLeft(SnakeHead.left()-10);
        SnakeHead.setRight(SnakeHead.right()-10);
        break;
    case 4:
        SnakeHead.setLeft(SnakeHead.left()+10);
        SnakeHead.setRight(SnakeHead.right()+10);
        break;
    default:;

    }
    vSnakeRect[0]=SnakeHead;
    //是否撞墙
    if(SnakeHead.left()<20||SnakeHead.right()>580||SnakeHead.top()<20||SnakeHead.bottom()>580){
        blsOver=true;
        Display="游戏结束";
    }
    update();
}
void tann::keyPressEvent(QKeyEvent*event){
    QKeyEvent *key=(QKeyEvent*) event;
    switch(key->key()){
    case Qt::Key_Up:nDirection=1;
        break;
    case Qt::Key_Down:nDirection=2;
        break;
    case Qt::Key_Left:nDirection=3;
        break;
    case Qt::Key_Right:nDirection=4;
        break;
    default:;
    }
}
QRect tann::CreateFood(){
    int x,y;
    x=rand()%56;
    y=rand()%56;
    QRect rect(20+x*10,20+y*10,10,10);
    return rect;
}
void tann::IsEat(){
    if(SnakeHead==Food){
        SnakeHead=Food;
        vSnakeRect.push_back(vSnakeRect.last());
        Food=CreateFood();
        Score=Score+10;
        Speed=Speed-20;
        timer->stop();
        timer->start(Speed);
    }

}
void tann::IsHit(){
    for(int i=1;i<vSnakeRect.size();i++){
        if(SnakeHead==vSnakeRect[i]){
            Display="游戏结束";
            blsOver=true;
            update();//刷新界面
        }
    }
}


